console.log('我是main.js 2')

getCSS.onclick = () => {
  const request = new XMLHttpRequest()
  request.open('GET', '/style.css')
  request.onreadystatechange = () => {
    if(request.readyState === 4){
      if(request.status >=200 && request.status < 300){
        const style = document.createElement('style')
        style.innerHTML = request.response
        document.head.appendChild(style)        
      }else{
        console.log('加载style.css失败了')
      }
    }
  }
  request.send()  
}
getJS.onclick = () => {
  const request = new XMLHttpRequest()
  request.open('GET', './app.js')
  request.onload = () => {
    const script = document.createElement('script')
    script.innerHTML = request.response
    document.head.appendChild(script)
  }
  request.onerror = () => {
    console.log('加载app.js失败了')

  }
  request.send()
}
getHTML.onclick = () => {
  const request = new XMLHttpRequest()
  request.open('GET', './page.html')
  request.onload= () => {
    const div = document.createElement('div')
    div.innerHTML = request.response
    document.body.appendChild(div)
  }
  request.onerror = () => {
    console.log('加载page.html失败了')
  }
  request.send()
}
getXML.onclick = () => {
  const request = new XMLHttpRequest()
  request.open('GET', './info.xml')
  request.onreadystatechange = () => {
    if(request.readyState === 4 && request.status === 200) {
      const dom = request.responseXML
      const text = dom.getElementsByTagName('warning')[0].textContent
      console.log(text.trim())
    }
  }
  request.send()
}
getJSON.onclick = () => {
  const request = new XMLHttpRequest()
  request.open("GET", './data.json')
  request.onreadystatechange = () => {
    if(request.readyState === 4 && request.status === 200) {
      const object = JSON.parse(request.response)
      myname.textContent = object.name
    }
  }
  request.send()
}
let n =1;

getPage.onclick = () => {
  const request = new XMLHttpRequest()
  request.open('GET', `/page${n+1}`)
  request.onreadystatechange = () => {
    if(request.readyState === 4 && request.status === 200) {
      const array = JSON.parse(request.response)
      array.forEach(item => {
        const li = document.createElement('li')
        li.textContent = item.id
        pageData.appendChild(li)
      })
      n = n + 1
    }
  }
  request.send()
}